//
//  StackViewLearnViewController.swift
//  WanAndroidLearn
//
//  Created by TRS-Mobile on 2023/11/3.
//

import Foundation
import UIKit
import RxSwift
import RxCocoa
import SnapKit
import Then


class StackViewLearnViewController: BaseViewController {
    lazy var stackView: UIStackView = {
        let view = UIStackView()
        view.backgroundColor = .lightGray
        return view
    }()
    
    let red = UIView().then {
        $0.backgroundColor = .red
        $0.snp.makeConstraints { make in
            make.width.equalTo(30)
        }
    }
    let yellow = UIView().then {
        $0.backgroundColor = .yellow
//        $0.snp.makeConstraints { make in
//
//        }
    }
    let blue = UIView().then {
        $0.backgroundColor = .blue
        $0.snp.makeConstraints { make in
            make.width.equalTo(30)
        }
    }
    let green = UIView().then {
        $0.backgroundColor = .green
        $0.snp.makeConstraints { make in
            make.width.equalTo(30)
        }
    }
    
    let space = UIView().then {
        $0.backgroundColor = .white
        $0.snp.makeConstraints { make in
            make.width.equalTo(10)
        }
    }
    
    lazy var remove: UIButton = {
        let bt = UIButton(type: .system).then {
            $0.setTitle("点击消失", for: .normal)
            view.addSubview($0)
            $0.snp.makeConstraints { make in
                make.left.equalToSuperview().inset(20)
                make.top.equalTo(stackView.snp.bottom).offset(20)
            }
        }
        return bt
    }()
    
    lazy var add: UIButton = {
        let bt = UIButton(type: .system).then {
            $0.setTitle("点击增加", for: .normal)
            view.addSubview($0)
            $0.snp.makeConstraints { make in
                make.left.equalTo(remove.snp.right).offset(20)
                make.top.equalTo(stackView.snp.bottom).offset(20)
            }
        }
        return bt
    }()
    
    override func makeUI() {
        super.makeUI()
        view.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalToSuperview().inset(20)
            make.height.equalTo(50)
        }
        stackView.axis = .horizontal
        stackView.distribution = .fill
        stackView.addArrangedSubviews([red, yellow, blue, green])
        stackView.spacing = 10
        
//        stackView.setCustomSpacing(50, after: red)
        
        remove.rx.tap
            .subscribe(onNext: { [unowned self] _ in
                stackView.removeArrangedSubview(red)
            })
            .disposed(by: disposeBag)
        
        add.rx.tap
            .subscribe(onNext: { [unowned self] _ in
                stackView.insertArrangedSubview(red, at: 0)
//                stackView.setCustomSpacing(50, after: red)
            })
            .disposed(by: disposeBag)
    }
}
